import {
  list, summary, depositList
} from '../../../api/shareCenter';

const util = require('../../../utils/util.js')
let app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
	memberInfo: {},
	member: 0,
	rebate: 0,
	pageNum: 1,
	pageSize: 10,
	recordList: [],
	loading: true,
	iconUrl: ["wallet-consumption.png", "wallet-income.png"],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
  },

  initShareCenter () {
  	list().then(res => {
  		let memberInfo = res.data
		memberInfo.balance = memberInfo.balance.toFixed(2)
		memberInfo.freezeBalance = memberInfo.freezeBalance.toFixed(2)

		this.setData({ memberInfo })
  	}).catch(err => { console.log(err) });
	/* summary().then(res => {
	  console.log('订单',res)
	  let { member, rebate} = res.data
	  rebate = rebate.toFixed(2)
	  this.setData({ member, rebate })
	}).catch(err => { console.log(err) }) */

	this.getRecordList();
  },

  getRecordList () { // 加载账单记录
	this.data.loading = false
	let params = {
		pageNum: this.data.pageNum,
		pageSize: this.data.pageSize
	}
    depositList(params).then(res => {
      console.log(res)
	  let { recordList } = this.data
	  res.data.data.forEach(item => {
		  item.createDate = util.datetimeFormat(item.createDate)
		  item.debit = Number(item.debit).toFixed(2);
		  item.credit = Number(item.credit).toFixed(2);
		  item.balance = Number(item.balance).toFixed(2);
		  if (item.type == 0) {
			  item.icon = this.data.iconUrl[1]
		  } else if (item.type == 1) {
			  item.icon = this.data.iconUrl[1]
		  } else if (item.type == 2) {
			  item.icon = this.data.iconUrl[0]
		  } else if (item.type == 3) {
			  item.icon = this.data.iconUrl[0]
		  } else if (item.type == 4) {
			  item.icon = this.data.iconUrl[1]
		  } else if (item.type == 5) {
			  item.icon = this.data.iconUrl[1]
		  } else if (item.type == 6) {
			  item.icon = this.data.iconUrl[0]
		  }
		  recordList.push(item)
	  })
      this.setData({ recordList })
	  this.data.loading = true
	  wx.hideLoading()
	  // 达到最大页数则不加载
	  let maxPageNum = Math.ceil(res.data.total / this.data.pageSize)
	  if (this.data.pageNum >= maxPageNum) {
	  		this.data.loading = false
	  }
    }).catch(err => {
      console.log(err)
	  this.data.loading = true
    })
  },

  goCashOut () {
	wx.navigateTo({
		url:'/pages/member/cashOut/cashOut'
	})
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
	this.setData({ recordList: [], pageNum: 1 })
	this.initShareCenter()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.loading == true) {
      this.data.pageNum = this.data.pageNum + 1;
      wx.showLoading({
        title: '加载中',
      })
      this.getRecordList()
    } else {
      wx.showToast({
        title: '已没有更多',
              icon: "none",
        duration: 2000
      })
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})
